// Written by Steve Andersen, copyright (c) 2007 ONE/Northwest
// This program is released under the GNU General Public License. http://www.gnu.org/licenses/
public class ONEN_Test_HouseholdBulk {
	 
	public static testMethod void testBulkContactAdd () {

		// for a single contact w/ no previous mbrships, add a new membership
		// and test mbr stats are created 
	
		integer howMany = 50;
		Date datClose = System.Today();
			
		// create & insert contact(s)
		Contact[] TestCons = ONEN_TEST_UnitTestData.CreateMultipleTestContacts ( howMany );
		Test.StartTest();
		insert TestCons; 
		Test.StopTest();
		//now test that a contact has received the proper member stats from the trigger
		
		Contact newCon = [SELECT Id,FirstName, LastName, ONEN_Household__c, Household_Name__c FROM Contact WHERE FirstName = 'Number1'];
		Id FirstConId = newCon.Id;
		String FirstConFirstName = newCon.FirstName;
		String FirstConLastName = newCon.LastName;
		Id FirstConHHId = newCon.ONEN_Household__c;
		System.AssertEquals ( FirstConFirstName + ' ' + FirstConLastName, newCon.Household_Name__c);
		
	}
	
	public static testMethod void testBulkContactChange () {
	
		// for a single contact w/ no previous mbrships, add a new membership
		// and test mbr stats are created
	
		integer howMany = 50;
		Date datClose = System.Today();
			
		// create & insert contact(s)
		Contact[] TestCons = ONEN_TEST_UnitTestData.CreateMultipleTestContacts ( howMany );
		
		insert TestCons;
		
		//now test that a contact has received the proper member stats from the trigger
		Contact newCon = [SELECT Id,FirstName, LastName, ONEN_Household__c, Household_Name__c FROM Contact WHERE FirstName = 'Number1'];
		Id FirstConId = newCon.Id;
		
		String FirstConFirstName = newCon.FirstName;
		String FirstConLastName = newCon.LastName;
		
		//system.debug(FirstConFirstName);
		//system.debug(FirstConLastName);
		
		
		Id FirstConHHId = newCon.ONEN_Household__c;
		System.AssertEquals ( FirstConFirstName + ' ' + FirstConLastName, newCon.Household_Name__c);
		
		list<Id> newConIds = new list<Id>();
		
		for (Contact thisNewCon : TestCons) {
			newConIds.add(thisNewCon.Id);
		}
		Contact[] newConsList = [SELECT Id, FirstName, LastName, ONEN_Household__c FROM Contact WHERE Id IN :newConIds order by FirstName];
		//system.debug(newConsList.size());
		
		// create & insert contact(s)
		Contact[] TestSecondCons = ONEN_TEST_UnitTestData.CreateMultipleTestContactsInHouseholds (newConsList) ;
		//system.debug(TestSecondCons);
		Test.StartTest();
		insert TestSecondCons;
		Test.StopTest();
		
		Contact[] allContacts = [select firstname, lastname, onen_Household__r.Name, ONEN_Household__r.Greeting__c, Household_Name__c, Household_Greeting__c from Contact where lastname='Doppelganger'];
		for(Contact c : allContacts) {
			system.assertEquals(c.onen_Household__r.Name,c.Household_Name__c);
			system.assertNotEquals(null,c.Household_Name__c);
			system.assertNotEquals(null,c.onen_Household__c);
		}
		
		Contact[] newCon2 = [SELECT FirstName, LastName, ONEN_Household__r.Name,Household_Name__c FROM Contact WHERE ONEN_Household__c  = :FirstConHHId];
		
		
		//system.debug(newCon2);
		String contactFirstName=newCon2[0].FirstName;
		String contactLastName=newCon2[0].LastName;
		String secondContactFirstName=newCon2[1].FirstName;
		String secondContactLastName=newCon2[1].LastName;
		String hhName = contactFirstName + ' and ' + secondContactFirstName + ' ' + contactLastName;
		System.AssertEquals ( 2, newCon2.size());
		
		system.AssertEquals(contactFirstName,newCon2[0].FirstName);
		system.AssertEquals(hhName,newCon2[0].ONEN_Household__r.Name);
		system.AssertEquals(secondContactFirstName,newCon2[1].FirstName);
		system.AssertEquals(hhName,newCon2[0].Household_Name__c);
		system.AssertEquals(hhName,newCon2[1].Household_Name__c);
		//System.AssertEquals ( 'what should it be?', newCon2[0].Household_Name__c);
		
		
		
		/*
		
		Integer counter = 0;
		for ( Contact thisContact : TestCons) {
			thisContact.FirstName = 'NewName'+counter;
			thisContact.LastName = 'LastName';
			counter++;
		}

		// insert the opp(s)
		Test.StartTest();
		update TestCons;
		Test.StopTest();
		id eleventhConId = TestCons[11].id;
		Contact updatedCon = [SELECT FirstName, LastName, Household_Name__c FROM Contact WHERE Id = :eleventhConId];
		System.AssertEquals ( 'NewName11', updatedCon.FirstName);
		System.AssertEquals ( 'LastName', updatedCon.LastName);
		System.AssertEquals ( 'NewName11 LastName', updatedCon.Household_Name__c);
		
		*/
	}
	

}